﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Drawing; 
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

using Danser_AJAXEnabledWebApplication.Faults;
using Danser_AJAXEnabledWebApplication.User_Controls;
using Danser_Control.Managers;
using Danser_Entity.Entities;

namespace Danser_AJAXEnabledWebApplication.Query_Popups
{
    /// <summary>
    /// Pop up page for selecting a user query to execute
    /// </summary>
    public partial class LoadQuery : System.Web.UI.Page
    {
        #region Constants;

        //session
        /// <summary>
        /// String
        /// </summary>
        public const String SESSION_USER_ID = "LoadQueriesUserID";
        /// <summary>
        /// String
        /// </summary>
        public const String SESSION_SELECTED_QUERY = "selectedQueryString";
     
        /// <summary>
        /// String
        /// </summary>
        private const String SESSION_DATA_TABLE = "queriesDataTable";

        //grid controls
        /// <summary>
        /// String
        /// </summary>
        private const String ROW_SELECTOR_ID = "rowSelector";

        //table columns
        /// <summary>
        /// String
        /// </summary>
        private const String NAME_COL = "Name";
        /// <summary>
        /// String
        /// </summary>
        private const String QUERY_COL = "Query";
        /// <summary>
        /// String
        /// </summary>
        private const String DESCRIPTION_COL = "Description";

        #endregion

        #region Page Load:

        /// <summary>
        /// loads the page...
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)// first Time
            {
                String userId = Session[SESSION_USER_ID] as String;
                IQueryManager queryManager = ManagerFactory.QueryManager();

                try
                {
                    IList<UserQuery> userQueries = queryManager.GetUserFavoriteQueries(userId);

                    DataTable queriesTable = InitiateTable();
                    FeedDataToTable(userQueries, queriesTable);

                    Session[SESSION_DATA_TABLE] = queriesTable;

                    queriesGridView.DataSource = Session[SESSION_DATA_TABLE] as DataTable;
                    queriesGridView.DataBind();
                }
                catch (Exception exception)
                {
                    Session[GenericErrorPage.SESSION_ERROR_KEY] = exception;
                    Response.Redirect(GenericErrorPage.SESSION_ERROR_PAGE_URL_KEY);
                }

            }
        }

        #endregion

        #region Heplers:

        private DataTable InitiateTable()
        {
            DataTable myDataTable = new DataTable();
            DataColumn myDataColumn = null;

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = NAME_COL;
            myDataTable.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = QUERY_COL;
            myDataTable.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = DESCRIPTION_COL;
            myDataTable.Columns.Add(myDataColumn);

            return myDataTable;
        }

        private void FeedDataToTable(IList<UserQuery> queries, DataTable table)
        {
            foreach (UserQuery query in queries)
            {
                DataRow newRow = table.NewRow();
                newRow[NAME_COL] = query.Name;
                newRow[QUERY_COL] = query.QueryString;
                newRow[DESCRIPTION_COL] = query.Description;

                table.Rows.Add(newRow);
            }
        }

        #endregion

        #region Grid Handlers:

        /// <summary>
        /// event handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rowSelector_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton selectedCheckBox = sender as RadioButton;
            GridViewRow row = (selectedCheckBox.Parent.Parent) as GridViewRow;

            Session[SESSION_SELECTED_QUERY] = row.Cells[2/*=queryString*/].Text;

            //simulate only on selected
            foreach (GridViewRow gridRow in queriesGridView.Rows)
            {
                if (gridRow.RowType == DataControlRowType.DataRow)
                {
                    RadioButton rowSelector = gridRow.FindControl(ROW_SELECTOR_ID) as RadioButton;
                    rowSelector.Checked = gridRow.Equals(row);
                }
            }
        }

        /// <summary>
        /// event handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void queriesGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //decorate cells
            GridViewRow row = e.Row;
            for (int i = 1; i < row.Cells.Count; i++)
            {
                TableCell cell = row.Cells[i];
                double cellWidth = i == 2 ? 400 : 150;
                cell.Width = new Unit(cellWidth, UnitType.Pixel);
                cell.BorderColor = Color.White;
                cell.BorderStyle = BorderStyle.Solid;
                cell.BorderWidth = new Unit(1, UnitType.Pixel);
            }
        }

        /// <summary>
        /// event handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void queriesGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            queriesGridView.PageIndex = e.NewPageIndex;
            DataTable queriesTable = Session[SESSION_DATA_TABLE] as DataTable;
            queriesGridView.DataSource = queriesTable;
            queriesGridView.DataBind();
        }

        #endregion
    }
}
